home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************/
- /* whatis.c - a simple-minded whatis program for MiNT and TOS */
- /* Copyright (c) 1992 by HPP Biersma (schuller@dutiag.tudelft.nl) */
- /* This program comes under the GNU public license - */
- /* see the file "copying" for details. */
- /******************************************************************/
- /* bugs: - not UN*X compatible, either in source-code or behavior */
- /* - string operations are not safe, ie not limits checked */
- /******************************************************************/
- /* version: 0.2 (second released version), December 15, 1992 */
- /* written for: - GCC version 2.3.1, patchlevel 1 */
- /* - mintlib patchlevel 25 */
- /* compile with: gcc -o whatis.ttp whatis.c -mbaserel -mpcrel -O2 */
- /******************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stat.h>
- #include <mintbind.h>
-
- #define NAME_LENGTH 256
-
- /* prototypes for local (static) functions */
- static int check_directory_name(const char *dirname);
- static void strip_filename(const char *name, char *stripped_name);
- static int print_apropos(const char *mandir, const char *command);
-
- static char man_path[NAME_LENGTH] = "/usr/man";
-
- void main(int argc, char *argv[])
- {
- char *ptr, stripped_name[NAME_LENGTH];
- int done_anything, did_print;
-
- /* Set the search path for the man pages */
- if ((ptr = getenv("MANPATH")) != NULL)
- strncpy(man_path, ptr, NAME_LENGTH - 1);
-
- done_anything = 0;
- argc -= 1;
- argv += 1;
- while (argc >= 1)
- {
- ptr = argv[0];
- if (*ptr == '-')
- {
- if (*(ptr + 1) == 'M')
- {
- if (*(ptr + 2) != 0x00)
- strcpy(man_path, (ptr + 2));
- else
- {
- if (argc == 1)
- {
- fprintf(stderr, "whatis: -M needs an argument\n");
- exit(1);
- }
- else
- {
- strcpy(man_path, argv[1]);
- argv += 1;
- argc -= 1;
- }
- } /* End of if (-M (space) name) */
- } /* End of if (-M option) */
- else
- {
- fprintf(stderr, "whatis: no option %s supported\n", ptr);
- fprintf(stderr, "usage: whatis [-M path] command ...\n");
- exit(1);
- }
- } /* end of if (option) */
- else
- {
- char *path_ptr;
-
- /* walk along manual path and check whatis database */
- path_ptr = man_path;
- did_print = 0;
- while (*path_ptr != 0x00)
- {
- char mandir[NAME_LENGTH], *manptr;
-
- /* Copy one directory from the manual search path into mandir */
- manptr = mandir;
- while ((*path_ptr != 0x00) && (*path_ptr != ';') &&
- (*path_ptr != ','))
- *manptr++ = *path_ptr++;
- *manptr = 0x00;
- if (*path_ptr != 0x00)
- path_ptr += 1;
-
- if (check_directory_name(mandir) == 0)
- {
- fprintf(stderr, "whatis: directory %s on search path not found\n",
- mandir);
- continue; /* go on to next part of manual search path */
- }
-
- strip_filename(ptr, stripped_name);
- did_print |= print_apropos(mandir, stripped_name);
- done_anything = 1;
- } /* end of while (walk along manual search path) */
- if (did_print == 0)
- fprintf(stderr, "whatis: %s not found in whatis database\n", ptr);
- }
- argv += 1;
- argc -= 1;
- } /* End of while (arguments left) */
-
- if (done_anything == 0)
- {
- fprintf(stderr, "usage: whatis [-M path] command ...\n");
- exit(1);
- }
-
- exit(0);
- } /* End of main() */
-
-
- /* Check if a dirname is a valid directory */
- static int check_directory_name(const char *dirname)
- {
- struct stat dir_stat;
-
- if (stat(dirname, &dir_stat) == -1)
- return(0);
- else if (dir_stat.st_mode & S_IFDIR == 0)
- {
- fprintf(stderr, "whatis: %s is not a directory\n", dirname);
- return(0);
- }
- else
- return(1);
- } /* End of check_directory_name() */
-
-
- /* Strip the leading path-name and extension components from a filename. */
- /* This is needed for the `man -f' (whatis) option, which is the same as */
- /* `man -k' (apropos), except the whatis version needs a stripped name. */
- static void strip_filename(const char *name, char *stripped_name)
- {
- char buffer[NAME_LENGTH], *ptr1, *ptr2;
-
- strcpy(buffer, name);
- ptr1 = ptr2 = buffer;
- while (*ptr2 != 0x00)
- {
- if ((*ptr2 == '/') || (*ptr2 == '\\'))
- ptr1 = ptr2 + 1;
- ptr2 += 1;
- }
- ptr2 = ptr1;
- while ((*ptr2 != 0x00) && (*ptr2 != '.'))
- ptr2 += 1;
- *ptr2 = 0x00;
- strcpy(stripped_name, ptr1);
- } /* End of strip_filename() */
-
-
- /* Handle the `man -k' (apropos) search within a specific directory. */
- /* This is similar to the `man -f' (whatis) search, which also makes */
- /* use of this routine. The whatis argument must be stripped first. */
- static int print_apropos(const char *mandir, const char *name)
- {
- FILE *whatis;
- char line_read[NAME_LENGTH], line_buffer[NAME_LENGTH],
- name_buffer[NAME_LENGTH];
- int did_print;
-
- strcpy(name_buffer, mandir);
- strcat(name_buffer, "/whatis");
- if ((whatis = fopen(name_buffer, "r")) == NULL)
- {
- fprintf(stderr, "%s: no such file\n", name_buffer);
- return(0);
- }
-
- /* No case-insensitive strstr exists. Improvise with all-lowercase. */
- strcpy(name_buffer, name);
- strlwr(name_buffer);
- did_print = 0;
- while (!feof(whatis))
- {
- fgets(line_read, NAME_LENGTH - 1, whatis);
- strcpy(line_buffer, line_read);
- strlwr(line_buffer);
- if (strstr(line_buffer, name_buffer) != NULL)
- {
- fputs(line_read, stdout);
- did_print = 1;
- }
- }
- fclose(whatis);
- return(did_print);
- } /* End of print_apropos() */
-